const RouterHook = require("../module/routerModule")
const validation = require('../util/Validation')
// const resVal = require('../util/ResValue')
const {
    AssertUtils
} = require("../util/utils");

const router = RouterHook()
const {
    getUserList,
    delUserList,
    addUserList,
    updateUserList
} = require('../service/adminUser')

//用户后台管理接口
router.get('/list', async (ctx) => {
    const { username, phone, name } = ctx.query
    const result = await getUserList(username, phone, name)
    return ctx.result.success(result)
})

router.get('/remove', async (ctx) => {
    const { id } = ctx.query
    const res = await validation.theFieldIsMissingExists(ctx.query, ['id'])
    AssertUtils.StringNotEmpty(id, res)
    const result = await delUserList(id)
    return ctx.result.success(result)
})

router.post('/add', async (ctx) => {
    const {
        username, password, phone, name, avatar, money
    } = ctx.request.body
    const body = ctx.request.body
    const res = await validation.theFieldIsMissingExists(body, ['username', 'password', 'phone', 'name', 'avatar', 'money'])
    AssertUtils.isFalse(res, res)
    const result = await addUserList(username, password, phone, name, avatar, money)
    return ctx.result.success(result)
})

router.post('/update', async (ctx) => {
    const {
        id,username, password, phone, name, avatar, money
    } = ctx.request.body
    const body = ctx.request.body
    const res = await validation.theFieldIsMissingExists(body, ['username', 'password', 'phone', 'name', 'avatar', 'money', 'id'])
    AssertUtils.isFalse(res, res)
    const result = await updateUserList(id, username, password, phone, name, avatar, money)
    return ctx.result.success(result)
}).noAuth()


module.exports = router